unit uFrmCancelOperation;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, gnugettext, Oracle;

type
  TFrmCancelOperation = class(TForm)
    btn_Cancel: TButton;
    btn_Ok: TButton;
    rad_CancelOperation: TRadioGroup;
    Label1: TLabel;
    lbl_PlayerName: TLabel;
    OracleQuery1: TOracleQuery;
    procedure btn_OkClick(Sender: TObject);
    procedure btn_CancelClick(Sender: TObject);
    procedure rad_CancelOperationClick(Sender: TObject);
  private
    m_SelectedRadioIdx: integer;
    m_TicketId: string;
    m_IsReprint, m_IsCashGame: boolean;
    function GetID(Query: string): string;
    { Private declarations }
  public
    procedure ShowForm(TicketId, PlayerName: string; IsReprint: boolean; IsCashGame: boolean);
    { Public declarations }
  end;

var
  FrmCancelOperation: TFrmCancelOperation;

implementation

uses umainutils, uprintticket, uprintwin, uutils, ufrmmain;
{$R *.dfm}

procedure TFrmCancelOperation.btn_CancelClick(Sender: TObject);
begin
  close();
end;

procedure TFrmCancelOperation.btn_OkClick(Sender: TObject);
var
  BuyInID, RebuyID, AddonID, WinId, FromTableId, TournamentID, TableID: string;
begin
  if m_IsReprint then
  begin
    case m_SelectedRadioIdx of
      0:
        begin
          FrmPrintTicket.SetData(false, m_TicketId, TicketTypeBuy, OraSessMainUtils);
        end;
      1:
        begin
          RebuyID := GetID('select id from (select id, datetime from TICKET_REBUY where ticket_id=' + m_TicketId + ' order by datetime desc) where rownum=1');
          FrmPrintTicket.SetData(false, RebuyID, TicketTypeReBuy, OraSessMainUtils);
        end;
      2:
        begin
          AddonID := GetID('select id from (select id, datetime from TICKET_ADDON where ticket_id=' + m_TicketId + ' order by datetime desc) where rownum=1');
          FrmPrintTicket.SetData(false, AddonID, TicketTypeAddon, OraSessMainUtils);
        end;
      4:
        begin
          WinId := m_TicketId;
          FrmPrintWin.SetData(m_TicketId, OraSessMainUtils);
        end;
    end;
    close();
  end
  else
  begin
    if MessageDlg(_('Confirm cancelling operation:') + ' ' + rad_CancelOperation.Items[m_SelectedRadioIdx], mtConfirmation, [mbYes, mbNo], 0) = mrYes then
    begin
      if not m_IsCashGame then
      begin
        case m_SelectedRadioIdx of
          0:
            begin
              BuyInID := m_TicketId;
              UpdateDataToDB('TICKETS', 'DELETED=1', 'ID=' + BuyInID);
              OraQueryMainUtils.close;
              OraQueryMainUtils.SQL.Text := 'SELECT TOURNAMENT_ID, TABLE_ID FROM TICKETS WHERE ID=' + m_TicketId;
              OraQueryMainUtils.Execute;
              TournamentID := OraQueryMainUtils.Field(0);
              TableID := OraQueryMainUtils.Field(1);
              frmmain.SendEventToServer(EventTypeFromTable, TournamentID, TableID);
            end;
          1:
            begin
              RebuyID := GetID('select id from (select id, datetime from TICKET_REBUY where ticket_id=' + m_TicketId + ' order by datetime desc) where rownum=1');
              umainutils.DeleteDataFromDB('TICKET_REBUY', 'ID=' + RebuyID);
            end;
          2:
            begin
              AddonID := GetID('select id from (select id, datetime from TICKET_ADDON where ticket_id=' + m_TicketId + ' order by datetime desc) where rownum=1');
              umainutils.DeleteDataFromDB('TICKET_ADDON', 'ID=' + AddonID);
            end;
          3:
            begin
              FromTableId := m_TicketId;
              umainutils.UpdateDataToDB('TICKETS', 'WIN_POSITION=NULL, OUT_DATETIME=NULL', 'ID=' + FromTableId);
              OraQueryMainUtils.close;
              OraQueryMainUtils.SQL.Text := 'SELECT TOURNAMENT_ID, TABLE_ID FROM TICKETS WHERE ID=' + m_TicketId;
              OraQueryMainUtils.Execute;
              TournamentID := OraQueryMainUtils.Field(0);
              TableID := OraQueryMainUtils.Field(1);
              frmmain.SendEventToServer(EventTypeFromTable, TournamentID, TableID);
            end;
          4:
            begin
              WinId := m_TicketId;
              umainutils.UpdateDataToDB('TICKETS', 'PAY_VALUE=NULL', 'ID=' + WinId);
              OraQueryMainUtils.close;
              OraQueryMainUtils.SQL.Text := 'SELECT TOURNAMENT_ID, TABLE_ID FROM TICKETS WHERE ID=' + m_TicketId;
              OraQueryMainUtils.Execute;
              TournamentID := OraQueryMainUtils.Field(0);
              TableID := OraQueryMainUtils.Field(1);
              frmmain.SendEventToServer(EventTypeFromTable, TournamentID, TableID);
            end;
        end;
      end
      else
      begin
        // cash game
        case m_SelectedRadioIdx of
          0:
            begin
              UpdateDataToDB('CASH_GAME_TICKETS', 'DELETED=1', 'ID=' + m_TicketId);
            end;
          1:
            begin
              umainutils.DeleteDataFromDB('CASH_GAME_TICKET_REBUYS', 'CASH_GAME_TICKET_ID=' + m_TicketId);
            end;
          2:
            begin
              // addon
            end;
          3:
            begin
              // player out
              UpdateDataToDB('CASH_GAME_TICKETS', 'OUTPUT_VALUE=NULL, OUT_DATETIME=NULL', 'ID=' + m_TicketId);
            end;
          4:
            begin
              // pay value
            end;
        end;
      end;
      close();
    end;
  end;
end;

function TFrmCancelOperation.GetID(Query: string): string;
begin
  OracleQuery1.close;
  OracleQuery1.SQL.Text := Query;
  OracleQuery1.Execute;
  if OracleQuery1.Eof then
    result := '-1'
  else
    result := OracleQuery1.Field(0);
end;

procedure TFrmCancelOperation.rad_CancelOperationClick(Sender: TObject);
begin
  m_SelectedRadioIdx := rad_CancelOperation.ItemIndex;
end;

procedure TFrmCancelOperation.ShowForm(TicketId, PlayerName: string; IsReprint: boolean; IsCashGame: boolean);
begin
  m_TicketId := TicketId;
  m_IsReprint := IsReprint;
  m_IsCashGame := IsCashGame;
  lbl_PlayerName.Caption := PlayerName;
  rad_CancelOperation.ItemIndex := -1;
  // -----
  OracleQuery1.Session := OraSessMainUtils;
  OracleQuery1.close;
  if IsCashGame then
  begin
    OracleQuery1.SQL.Text :=
      ' SELECT COUNT(CGT.ID), COUNT(CGTR.REBUY_ID), COUNT(CGT.OUTPUT_VALUE) FROM CASH_GAME_TICKETS CGT LEFT JOIN (SELECT MAX(ID) AS REBUY_ID, CASH_GAME_TICKET_ID FROM CASH_GAME_TICKET_REBUYS GROUP BY CASH_GAME_TICKET_ID) '
      + ' CGTR ON CGT.ID=CGTR.CASH_GAME_TICKET_ID where cgt.id=' + TicketId;
    OracleQuery1.Execute;
    // -----
    if not OracleQuery1.Eof then
    begin
      rad_CancelOperation.Controls[1].Enabled := StrToB(OracleQuery1.Field(1)); // REBUY
      rad_CancelOperation.Controls[2].Enabled := false; // ADDON
      rad_CancelOperation.Controls[3].Enabled := StrToB(OracleQuery1.Field(2)); ; // FROMTABLE
      rad_CancelOperation.Controls[4].Enabled := false; // WIN
    end;
  end
  else
  begin
    OracleQuery1.SQL.Text := ' select ti.id, case when win_position is not null then 1 else 0 end as win_pos, case when pay_value is not null then 1 else 0 end as pay_value,' +
      ' case when count(ta.id) = 0 then 0 else 1 end as addon,case when count(tr.id) = 0 then 0 else 1 end as rebuy, case when out_datetime is null then 0 else 1 end as player_out ' +
      ' from tickets ti left join TICKET_ADDON ta on ti.id=ta.ticket_id left join TICKET_REBUY tr on ti.id=tr.ticket_id' + ' where ti.deleted=0 and ti.id=' + TicketId +
      ' group by ti.id,win_position, pay_value, out_datetime';
    OracleQuery1.Execute;
    // -----
    if not OracleQuery1.Eof then
    begin
      rad_CancelOperation.Controls[1].Enabled := StrToB(OracleQuery1.Field(4)); // REBUY
      rad_CancelOperation.Controls[2].Enabled := StrToB(OracleQuery1.Field(3)); // ADDON
      rad_CancelOperation.Controls[3].Enabled := StrToB(OracleQuery1.Field(5)); // FROMTABLE
      rad_CancelOperation.Controls[4].Enabled := StrToB(OracleQuery1.Field(2)); // WIN
    end;
  end;

  showmodal;
end;

end.
